import React, { Component } from 'react'
import axios from 'axios'

export default class Login extends Component {
    //获取输入内容
    matter = (value,key) =>{
        let { ruleFrom } = this.state
        ruleFrom[key] = value
        this.setState = {
            ruleFrom
        }
    }

    //登录
    login = (ruleFrom) => {
        axios({
            url:'/pc/login',
            method:'post',
            data:{
                username:ruleFrom.username,
                password:ruleFrom.password,
            }
        }).then(res => {
            if(res.data.code === 1){
                console.log(111);
                sessionStorage.setItem('token', res.data.token)
                sessionStorage.setItem('name', res.data.datas[0].name)
                sessionStorage.setItem('url', res.data.datas[0].url)
                sessionStorage.setItem('type', res.data.datas[0].type)

                //跳转
                this.props.history.push('/main')
            }else{
                console.log('失败了');
                
            }
        })
    }

    state = {
        ruleFrom:{}
    }
    render() {
        const { ruleFrom } = this.state
        return (
        <div className="Login">
            <div className="Login-box">
                <p>
                    账号：
                    <input type="text" value={ruleFrom.username} onChange={(e)=>this.matter(e.target.value,'username')}/>
                </p>
                <p>
                    密码：<input type="text" value={ruleFrom.password} onChange={(e)=>this.matter(e.target.value,'password')}/>
                </p>
                <button onClick={()=>this.login(ruleFrom)} className="loginbtn">登录</button>
            </div>
        </div>
        )
    }
}
